package com.lxt.basicmvc.base;

import android.app.ActivityManager;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Handler;
import android.webkit.WebView;

import androidx.multidex.BuildConfig;
import androidx.multidex.MultiDexApplication;

import com.blankj.utilcode.util.ToastUtils;
import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.Logger;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author joyson
 * @create 2020-09-29
 */
public class App extends MultiDexApplication {
    public static Context CONTEXT;
    private static Handler mhandler;

    private static final String process= BuildConfig.APPLICATION_ID;
    @Override
    public void onCreate() {
        super.onCreate();
        //修改webview 具体的进程存储路径
        initPieWebView();
        CONTEXT = this;
        mhandler = new Handler();

        Logger.addLogAdapter(new AndroidLogAdapter());
//        CrashReport.initCrashReport(APP_CONTEXT, "c1fe2c8b7f", true, strategy);

        //设置为默认，不跟随系统配置
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());

    }

    private void initPieWebView() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = getProcessName(this);
            if (!process.equals(processName)) {
                WebView.setDataDirectorySuffix(getString(processName, "ybon"));
            }
        }
    }
    public String getProcessName(Context context) {
        if (context == null) return null;
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo.pid == android.os.Process.myPid()) {
                return processInfo.processName;
            }
        }
        return null;
    }

    public String getString(String s, String defValue) {
        return isEmpty(s) ? defValue : s;
    }

    public boolean isEmpty(String s) {
        return s == null || s.trim().length() == 0;
    }


    public static Handler getMainHandler() {
        return mhandler;
    }

    /**
     * 在主线程中刷新UI的方法
     *
     * @param r
     */
    public static void runOnUIThread(Runnable r) {
        App.getMainHandler().post(r);
    }

    @Override
    public void onTrimMemory(int level) {
        super.onTrimMemory(level);
//        if (level == TRIM_MEMORY_UI_HIDDEN) {
//            GlideUtils.clearMemory(APP_CONTEXT);
//        }
//        Glide.get(APP_CONTEXT).trimMemory(level);
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
//        GlideUtils.clearMemory(APP_CONTEXT);
    }

    //设置字体为默认大小，不随系统字体大小改而改变
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }


    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }
}
